<template>
  <div class="history flex flex-col">
    <div class="head">
      <div>智能搜索</div>
    </div>
    <template v-if="results.length > 0">
      <ul class="flex flex-col flex-1">
        <li
          v-for="(item, key) in results"
          :key="key"
          class="flex justify-between align-center"
        >
          <div class="flex gc-4 align-center">
            <template v-if="item.type === 'folder'">
              <img :src="folderClose" width="24" />
            </template>
            <template v-else>
              <img :src="getWebIcon(item.link || '')" width="16" />
            </template>
            <div class="web-title">{{ item.title }}</div>
          </div>
          <div v-if="item.type === 'file'" class="link">
            <a target="_blank" :href="item.link">{{ item.link }}</a>
          </div>
          <div>
            <button
              v-if="item.type === 'file'"
              class="icon-button"
              @click="copy(item.link || '')"
            >
              <img :src="copyPng" width="16" />
            </button>
            <button v-else class="core-button open" @click="open(item)">
              打开
            </button>
          </div>
        </li>
        <hr />
      </ul>
    </template>
  </div>
</template>

<script setup lang="ts">
  import folderClose from '@/assets/images/folder-close.png';
  import copyPng from '@/assets/images/copy.png';
  import { ref } from 'vue';
  import { onClickOutside } from '@vueuse/core';
  import { Folder } from '@/types/global';
  import { currentFolder, getWebIcon } from '@/views/bus';
  import { copyText } from '@/utils/utils';
  import { message } from '@/views/components/message/bus';

  const emit = defineEmits(['update:visible']);
  const props = defineProps<{
    results: Folder[];
  }>();

  const copy = (link: string) => {
    copyText(link);
    message('已复制');
  };

  const open = (item: Folder) => {
    currentFolder.value = item;
  };
</script>

<style lang="scss" scoped>
  .history {
    width: 100%;
    height: 100%;

    ul {
      background-color: white;
      overflow-y: auto;
      li {
        padding: 8px 12px;
        border-radius: var(--radius);
        &:hover {
          background-color: var(--primary);
        }
      }
    }
    .head {
      line-height: 24px;
      padding: 6px 12px;
    }

    .web-title {
      max-width: 70px;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .open {
      background-color: #eee;
      &:hover {
        background-color: white;
        color: var(--blue);
      }
    }
    .link {
      max-width: 160px;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
</style>
